﻿using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using ImageMagick;

namespace apitest.Application
{
 
    public class ImageTestService
    {
        [HttpGet]
        public async Task<dynamic> T1()
        {

            using (var image = Image.Load<Rgba32>("img/222.jpg"))
            {
                // 调整图像大小  
                image.Mutate(x => x.Resize(1920, 1080));
                // image.Mutate(x => x.Grayscale());

                JpegEncoder encoder = new JpegEncoder()
                {
                    //标准中定义的0到100之间的质量值。默认值为75。
                    //通过减少Quality松散的信息，从而减小文件大小。
                    Quality = 50,
                    //IgnoreMetadata = true
                };


                // 保存调整大小后的图像  
                image.SaveAsJpeg("img/resized_image.jpg", encoder);
                return image.Metadata;
                //return new
                //{
                //    image.Width,
                //    image.Height,
                //    image.Bounds,
                //    image.PixelType,
                //    image.Metadata,
                //    image.Configuration,
                //    image.Frames,
                //};
            }
        }

        public async Task T2()
        {
            // Image dimensions of the gif.
            const int width = 100;
            const int height = 100;

            // Delay between frames in (1/100) of a second.
            const int frameDelay = 100;

            // For demonstration: use images with different colors.
            Color[] colors = {
    Color.Green,
    Color.Red
};

            // Create empty image.
            using Image<Rgba32> gif = new(width, height, Color.Blue);

            // Set animation loop repeat count to 5.
            var gifMetaData = gif.Metadata.GetGifMetadata();
            gifMetaData.RepeatCount = 5;

            // Set the delay until the next image is displayed.
            GifFrameMetadata metadata = gif.Frames.RootFrame.Metadata.GetGifMetadata();
            metadata.FrameDelay = frameDelay;
            for (int i = 0; i < colors.Length; i++)
            {
                // Create a color image, which will be added to the gif.
                using Image<Rgba32> image = new(width, height, colors[i]);

                // Set the delay until the next image is displayed.
                metadata = image.Frames.RootFrame.Metadata.GetGifMetadata();
                metadata.FrameDelay = frameDelay;

                // Add the color image to the gif.
                gif.Frames.AddFrame(image.Frames.RootFrame);
            }

            // Save the final result.
            gif.SaveAsGif("img/output.gif");
        }

        [HttpGet]
        public async Task T3()
        {
            using var image = new MagickImage("img/222.jpg");

            var size = new MagickGeometry(1920, 1080);
            // This will resize the image to a fixed size without maintaining the aspect ratio.
            // Normally an image will be resized to fit inside the specified size.
            size = new MagickGeometry(image.Width, image.Height);
            size.IgnoreAspectRatio = true;
            image.Quality = 20;
            image.Grayscale();
            image.Resize(size);

            // Save the result
            image.Write("img/Snakeware.100x100.jpg");
        }

        [HttpGet]
        public async Task T4()
        {
            using (var image = new MagickImage("img/222.jpg"))
            {
                using (var img = new MagickImage(new MagickColor("#ff00ff"), 512, 128))
                {
                    new Drawables()
                      .FontPointSize(72)
                      .Font("Comic Sans")
                      .StrokeColor(new MagickColor("yellow"))
                      .FillColor(MagickColors.Orange)
                      .TextAlignment(TextAlignment.Center)
                      .Text(256, 64, "Magick.NET")
                      .StrokeColor(new MagickColor(0, Quantum.Max, 0))
                      .FillColor(MagickColors.SaddleBrown)
                      .Ellipse(256, 96, 192, 8, 0, 360)
                      .Draw(img);
                    img.Write("img/sss.jpg");
                }
            }
        }
    }
}